'use strict';
import React, {
    Component
} from 'react'
import WxServer from '../Tools/WxServer';
import {
    HttpService,
    Tips
} from '../Tools'
export default class Test extends Component {
    constructor(props) {
        super(props)
    }
    componentDidMount() {
        WxServer.init();
        wx.onVoiceRecordEnd({
            complete: function(res) {
                sessionStorage.localId = res.localId;
                Tips(sessionStorage.localId)
            }
        });
    }

    wxstart() {
        wx.startRecord({
            success: function(){
                sessionStorage.startRecord = 'true';
            },
            cancel: function () {
                Tips('用户拒绝授权录音');
            }
        });
    }
    wxend() {
        wx.stopRecord({
            success: function(res) {
                console.log(JSON.stringify(res));
                sessionStorage.localId = res.localId;
            }
        });
    }
    wxbo() {
        Tips(sessionStorage.localId)
        wx.playVoice({
            localId: sessionStorage.localId // 需要播放的音频的本地ID，由stopRecord接口获得
        });
    }
    wxupload() {
        wx.uploadVoice({
            localId: sessionStorage.localId, // 需要上传的音频的本地ID，由stopRecord接口获得
            isShowProgressTips: 1, // 默认为1，显示进度提示
            success: function(res) {
                sessionStorage.serverId = res.serverId; // 返回音频的服务器端ID
            }
        });
    }
    wxdown() {
        wx.downloadVoice({
            serverId: sessionStorage.serverId, // 需要下载的音频的服务器端ID，由uploadVoice接口获得
            isShowProgressTips: 1, // 默认为1，显示进度提示
            success: function(res) {
                var localId = res.localId; // 返回音频的本地ID
            }
        });
    }

    render() {
        return (
            <div style={{fontSize:'30px'}}>
                <br /><br />
                <button style={{width:'100%'}} onClick={this.wxstart.bind(this)}>开始录音</button><br /><br />
                <button style={{width:'100%'}} onClick={this.wxend.bind(this)}>结束录音</button><br /><br />
                <button style={{width:'100%'}} onClick={this.wxbo.bind(this)}>播放录音</button><br /><br />
                <button style={{width:'100%'}} onClick={this.wxupload.bind(this)}>上传录音</button>
                <button style={{width:'100%'}} onClick={this.wxdown.bind(this)}>下载录音</button>
            </div>
        )
    }
}
